package com.qckj.amz.domain.spapi.feeds.home.ProductImagesFeedXml;

import com.qckj.amz.domain.ProductFmlBasic;
import com.qckj.amz.domain.ProductFmlRlat;
import com.qckj.amz.domain.ProductInit;
import com.qckj.amz.domain.spapi.feeds.home.AmazonEnvelope;
import com.qckj.amz.domain.spapi.feeds.home.Header;
import com.qckj.amz.domain.spapi.feeds.home.Message;
import com.qckj.amz.utils.constants.AmzConstants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 封装产品图片xml
 * @author luchuan
 * @version v1.0
 * @date 2022/11/20 0020
 **/
public class CreateProductImageFeedXml {

    /**
     *
     * @param sellerId          卖家记号
     * @param productInit       产品常用属性
     * @param listProduct       产品信息
     * @return
     */
    public String getProductImageFeedXml(String sellerId, ProductInit productInit, List<Map<String, Object>> listProduct) {

        if (listProduct==null && listProduct.isEmpty()){
            return null;
        }

        ArrayList<Message> messageList = new ArrayList<>();//封装多个Message，即多个sku产品，无论是有无变体或多个不同的产品
        for (Map<String, Object> mapProduct: listProduct) {
            ProductFmlBasic basicObj = (ProductFmlBasic)mapProduct.get("basicObj");
            List<ProductFmlRlat> rlatList = (List<ProductFmlRlat>)mapProduct.get("rlatList");
            //封装多个变体
            for (ProductFmlRlat rlat:rlatList) {

                String Main = rlat.getMainImageUrl() == null ? "" : rlat.getMainImageUrl();
                String PT1 = rlat.getOtherImageUrl1() == null ? "" : rlat.getOtherImageUrl1();
                String PT2 = rlat.getOtherImageUrl2() == null ? "" : rlat.getOtherImageUrl2();
                String PT3 = rlat.getOtherImageUrl3() == null ? "" : rlat.getOtherImageUrl3();
                String PT4 = rlat.getOtherImageUrl4() == null ? "" : rlat.getOtherImageUrl4();
                String PT5 = rlat.getOtherImageUrl5() == null ? "" : rlat.getOtherImageUrl5();
                String PT6 = rlat.getOtherImageUrl6() == null ? "" : rlat.getOtherImageUrl6();
                String PT7 = rlat.getOtherImageUrl7() == null ? "" : rlat.getOtherImageUrl7();
                String PT8 = rlat.getOtherImageUrl8() == null ? "" : rlat.getOtherImageUrl8();

                if (!Main.equals("")){
                    ProductImage productImageMain = new ProductImage();
                    productImageMain.setSKU(rlat.getItemSku());
                    productImageMain.setImageType("Main");
                    productImageMain.setImageLocation(rlat.getMainImageUrl());
                    Message messageMain = new Message();
                    messageMain.setMessageID(basicObj.getId());
                    messageMain.setOperationType(productInit.getUpdateDelete());
                    messageMain.setProductImage(productImageMain);
                    messageList.add(messageMain);
                }

                if (!PT1.equals("")){
                    ProductImage productImagePT1 = new ProductImage();
                    productImagePT1.setSKU(rlat.getItemSku());
                    productImagePT1.setImageType("PT1");
                    productImagePT1.setImageLocation(rlat.getOtherImageUrl1());
                    Message messagePT1 = new Message();
                    messagePT1.setMessageID(basicObj.getId());
                    messagePT1.setOperationType(productInit.getUpdateDelete());
                    messagePT1.setProductImage(productImagePT1);
                    messageList.add(messagePT1);
                }

                if (!PT2.equals("")){
                    ProductImage productImagePT2 = new ProductImage();
                    productImagePT2.setSKU(rlat.getItemSku());
                    productImagePT2.setImageType("PT2");
                    productImagePT2.setImageLocation(rlat.getOtherImageUrl2());
                    Message messagePT2 = new Message();
                    messagePT2.setMessageID(basicObj.getId());
                    messagePT2.setOperationType(productInit.getUpdateDelete());
                    messagePT2.setProductImage(productImagePT2);
                    messageList.add(messagePT2);
                }

                if (!PT3.equals("")){
                    ProductImage productImagePT3 = new ProductImage();
                    productImagePT3.setSKU(rlat.getItemSku());
                    productImagePT3.setImageType("PT3");
                    productImagePT3.setImageLocation(rlat.getOtherImageUrl3());
                    Message messagePT3 = new Message();
                    messagePT3.setMessageID(basicObj.getId());
                    messagePT3.setOperationType(productInit.getUpdateDelete());
                    messagePT3.setProductImage(productImagePT3);
                    messageList.add(messagePT3);
                }

                if (!PT4.equals("")){
                    ProductImage productImagePT4 = new ProductImage();
                    productImagePT4.setSKU(rlat.getItemSku());
                    productImagePT4.setImageType("PT4");
                    productImagePT4.setImageLocation(rlat.getOtherImageUrl4());
                    Message messagePT4 = new Message();
                    messagePT4.setMessageID(basicObj.getId());
                    messagePT4.setOperationType(productInit.getUpdateDelete());
                    messagePT4.setProductImage(productImagePT4);
                    messageList.add(messagePT4);
                }

                if (!PT5.equals("")){
                    ProductImage productImagePT5 = new ProductImage();
                    productImagePT5.setSKU(rlat.getItemSku());
                    productImagePT5.setImageType("PT5");
                    productImagePT5.setImageLocation(rlat.getOtherImageUrl5());
                    Message messagePT5 = new Message();
                    messagePT5.setMessageID(basicObj.getId());
                    messagePT5.setOperationType(productInit.getUpdateDelete());
                    messagePT5.setProductImage(productImagePT5);
                    messageList.add(messagePT5);
                }

                if (!PT6.equals("")){
                    ProductImage productImagePT6 = new ProductImage();
                    productImagePT6.setSKU(rlat.getItemSku());
                    productImagePT6.setImageType("PT6");
                    productImagePT6.setImageLocation(rlat.getOtherImageUrl6());
                    Message messagePT6 = new Message();
                    messagePT6.setMessageID(basicObj.getId());
                    messagePT6.setOperationType(productInit.getUpdateDelete());
                    messagePT6.setProductImage(productImagePT6);
                    messageList.add(messagePT6);
                }

                if (!PT7.equals("")){
                    ProductImage productImagePT7 = new ProductImage();
                    productImagePT7.setSKU(rlat.getItemSku());
                    productImagePT7.setImageType("PT7");
                    productImagePT7.setImageLocation(rlat.getOtherImageUrl7());
                    Message messagePT7 = new Message();
                    messagePT7.setMessageID(basicObj.getId());
                    messagePT7.setOperationType(productInit.getUpdateDelete());
                    messagePT7.setProductImage(productImagePT7);
                    messageList.add(messagePT7);
                }

                if (!PT8.equals("")){
                    ProductImage productImagePT8 = new ProductImage();
                    productImagePT8.setSKU(rlat.getItemSku());
                    productImagePT8.setImageType("PT8");
                    productImagePT8.setImageLocation(rlat.getOtherImageUrl8());
                    Message messagePT8 = new Message();
                    messagePT8.setMessageID(basicObj.getId());
                    messagePT8.setOperationType(productInit.getUpdateDelete());
                    messagePT8.setProductImage(productImagePT8);
                    messageList.add(messagePT8);
                }


            }
        }

        /**
         * 二级/Header
         */
        Header header = new Header();
        header.setDocumentVersion("1.01");
        header.setMerchantIdentifier(sellerId);

        /**
         * 一级/根节点AmazonEnvelope
         */
        AmazonEnvelope amazonEnvelope = new AmazonEnvelope();
        amazonEnvelope.setHeader(header);
        amazonEnvelope.setMessageType("ProductImage");//Product、Price、ProductImage
        amazonEnvelope.setMessage(messageList);

        /**
         * Javabean对象转换xml字符串
         */
        String xmlStr = AmzCommon.JavabeanToXmlStr(AmzConstants.FEED_TYPE_PRODUCT_IMAGES_FEED, amazonEnvelope);

        return xmlStr;
    }
}
